package com.neusoft.elmboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.UserService;

@RestController
@RequestMapping("/Users")
public class UserController {

	@Autowired
	private UserService userService;
	
	@GetMapping("/userId/{userId}/password/{password}")
	public User getUserByIdByPass(User user) throws Exception{
		return userService.getUserByIdByPass(user);
	}
	
	@GetMapping("/userId/{userId}")
	public int getUserById(User user) throws Exception{
		return userService.getUserById(user.getUserId());
	}
	
	@PostMapping("/userId/{userId}/password/{password}/userName/{userName}/userSex/{userSex}")
	public int saveUser(User user) throws Exception{
		return userService.saveUser(user);
	}
	
	@PutMapping("/userId/{userId}/userImg/{userImg}")
	public int updateUserImgById(User user) throws Exception{
		String img1 = user.getUserImg().replace("-", "/").replace("_", "+").replace("~", ";");
		user.setUserImg(img1);
		return userService.updateUserImgById(user);
	}
	
	@PutMapping("/userId/{userId}/userName/{userName}/userSex/{userSex}")
	public int updateUserById(User user) throws Exception{
		return userService.updateUserById(user);
	}
	
	@PutMapping("/userId/{userId}/password/{password}/newPass/{newPass}")
	public int updatePasswordById(@ModelAttribute User user,@PathVariable String newPass) throws Exception {
	    return userService.updatePasswordById(user, newPass);
	}
	
	@GetMapping("/businesses/userId/{userId}")
	public int IsBusiness(User user) throws Exception{
		return userService.IsBusiness(user.getUserId());
	}

}
